package com.infore.airsupervise.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.infore.airsupervise.R;
import com.infore.airsupervise.bean.LawingSiteE;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.db.SiteLawDBHelper;
import com.infore.airsupervise.ui.widget.ItemDecoration1px;
import com.infore.airsupervise.utils.JumpUtil;
import com.infore.airsupervise.utils.ToastUtil;
import com.rey.material.widget.CheckBox;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 草稿箱列表界面
 * Created by xiaotao on 2017/6/2.
 */

public class SiteDraftListActivity extends BaseTitleBarActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    private SiteLawDBHelper dbHelper;
    private List<LawingSiteE> siteEList = new ArrayList<>();
    private BaseQuickAdapter<LawingSiteE, BaseViewHolder> adapter;

    private boolean isEdit = false ; //列表编辑状态标识
    private List<LawingSiteE> deleteSelected =new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_site_draft);
        ButterKnife.bind(this);
        title.setText("草稿箱");
        rightMenu.setFontAwesomeIcon(FontAwesome.FA_ELLIPSIS_V);
        rightMenu.setVisibility(View.INVISIBLE);
        dbHelper = new SiteLawDBHelper(ctx, 1, UserE.getUserInfo().getUserId());
        initRecycleView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        siteEList.clear();
        siteEList.addAll(dbHelper.queryAll());
        adapter.notifyDataSetChanged();
        if(null == siteEList || siteEList.size()<=0){
            emptyListUIControl();
        }else {
            listUIControl();
        }
    }

    /**
     * 初始化 recyclerView
     */
    private void initRecycleView(){
        recyclerView.setLayoutManager( new LinearLayoutManager(ctx));
        recyclerView.addItemDecoration(new ItemDecoration1px(ctx,ItemDecoration1px.VERTICAL_LIST));
        adapter = new BaseQuickAdapter<LawingSiteE, BaseViewHolder>(R.layout.item_site_draft_list,siteEList) {
            @Override
            protected void convert(final BaseViewHolder helper, final LawingSiteE item) {
                helper.setText(R.id.tv_name,item.getName())
                        .setText(R.id.tv_time,item.getTime());
                //删除标记按钮
                final CheckBox checkBox = helper.getView(R.id.checkBox);
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        //操作要删除选项的缓存集合
                        if(isChecked){
                            deleteSelected.add(item);
                        }else {
                            deleteSelected.remove(item);
                        }
                    }
                });
                //每次不在编辑状态是重置每一个item的标记按钮
                if(!isEdit) {
                    checkBox.setChecked(false);
                }
                checkBox.setVisibility(isEdit ? View.VISIBLE : View.GONE);
                //item 长按监听
                helper.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        if(!isEdit){ //进入编辑删除状态
                            isEdit = true ;
                            notifyDataSetChanged();
                            entryEditUIControl();
                        }else {  //当前状态为编辑删除时
                            checkBox.setChecked(!checkBox.isChecked());
                        }
                        return true;
                    }
                });
                //item 点击
                helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!isEdit) {
                            JumpUtil.jumpFromTo(ctx, SiteMainCreateActivity.class, "LawingSiteE", item);
                        }else {
                            //当前状态为编辑删除时
                            checkBox.setChecked(!checkBox.isChecked());
                        }
                    }
                });
            }
        };
        recyclerView.setAdapter(adapter);
    }


    /**
     * 删除草稿记录的方法
     */
    private void menuDelete(){
        if(null == deleteSelected || deleteSelected.size() <= 0){
            ToastUtil.showShort(ctx,"无删除选项！");
        }else {
            //删除数据库存储的数据
            dbHelper.deleteAll(deleteSelected);
            //删除列表集合中对应的数据
            for (LawingSiteE e : deleteSelected) {
                siteEList.remove(e);
            }
            //清空临时保存要删除的数据集合
            deleteSelected.clear();
            //UI显示控制
            if (isEdit) {
                isEdit = false;
                adapter.notifyDataSetChanged();
                outEditUIControl();
            }
            if (null == siteEList || siteEList.size() <= 0) {
                emptyListUIControl();
            } else {
                listUIControl();
            }
        }
    }

    PopupMenu popupMenu = null ;
    /**
     * 右边导航栏按钮
     * @param view
     */
    @Override
    protected void onRightMenuClick(View view) {
        super.onRightMenuClick(view);
        if(isEdit) { // 打开编辑菜单
            if (null == popupMenu) {
                popupMenu = new PopupMenu(ctx, rightMenu);
                popupMenu.inflate(R.menu.site_draft_edit_menu);
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.menu_delete:
                                menuDelete();
                                break;
                        }
                        return true;
                    }
                });
            }
            popupMenu.show();
        }
    }

    /**
     * 左边导航栏按钮
     * @param view
     */
    @Override
    protected void onLeftMenuClick(View view) {
        if(isEdit) { //退出编辑状态
            isEdit = false ;
            adapter.notifyDataSetChanged();
            outEditUIControl();
        }else {
            super.onLeftMenuClick(view);
        }
    }

    /**
     *  退出编辑状态  显示隐藏UI
     */
    private void outEditUIControl(){
        leftMenu.setFontAwesomeIcon(FontAwesome.FA_CHEVRON_LEFT);
        rightMenu.setVisibility(View.INVISIBLE);
    }
    /**
     *  进入编辑状态  显示隐藏UI
     */

    private void entryEditUIControl(){
        rightMenu.setVisibility(View.VISIBLE);
        leftMenu.setFontAwesomeIcon(FontAwesome.FA_CLOSE);
    }
    /**
     * 列表为空时 UI
     */
    private void emptyListUIControl(){
        tvEmpty.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
    }
    /**
     * 列表不为空时 UI
     */
    private void listUIControl(){
        tvEmpty.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
    }

}
